<?php

namespace IDock\Insurance\TAIC\Request;

use IDock\Insurance\TAIC\Response\UnderwritingResponse;
use IDock\Insurance\TAIC\TAICEnums;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Visitor\NonCar\NonCarProductVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;

class UnderwritingRequest extends AllTAICRequest
{
    protected $route = 'access/car/insureConfirmation.mvc';

    protected $response = UnderwritingResponse::class;

    protected $urlTip = '投保单保存-核保';

    /**
     * @var  PremiumInquiryVisitor $visitor
     */
    protected $visitor;

    public function generate(): array
    {
        $this->tradeNo = $this->visitor->getExtInfoVisitor()->argc['tradeNo'] ?? '';

        $arr = [
            'cityCode' => $this->visitor->getExtInfoVisitor()->argc['cityCode'] ?? '',

            'rateMark' => '2',
            'carPremiumCaculateNo' => $this->visitor->getUnderwritingVisitor()->inquiryNo,
            'applyInfoDto' => $this->getApplicantData(),
            'insureInfoDto' => $this->getInsureData(),
            'carOwerDto' => $this->getOwnerData(),
            'deliveryDto' => $this->getDeliveryData(),
            //'businessNature' => $this->visitor->getExtInfoVisitor()->argc['businessNature'] ?? '',
            //'companyCode' => $this->visitor->getExtInfoVisitor()->argc['companyCode'] ?? '',
            //'solutionCode' => $this->visitor->getExtInfoVisitor()->argc['teamCode'] ?? '',
            //'salesmanCode' => $this->visitor->getExtInfoVisitor()->argc['salesmanCode'] ?? '',
            //'salesmanName' => $this->visitor->getExtInfoVisitor()->argc['salesmanName'] ?? '',
            //'intermediaryCode' => $this->visitor->getExtInfoVisitor()->argc['intermediaryCode'] ?? '',
        ];
        $arr = $this->nonCar($arr);
        return $arr;

    }

    /**
     * 投保人
     * @return array
     */
    private function getApplicantData(): array
    {
        $relationVisitor = $this->visitor->getApplicantVisitor();
        $data = [
            'insuredType' => $relationVisitor->typeCode == 'R1' ? '1' : '2',
            'applybirthDate' => '',
            'applyName' => $relationVisitor->name,
            'applyidentifyType' => TAICEnums::$certificateTypeCode[$relationVisitor->certificateTypeCode] ?? '',
            'applyidentifyNumber' => $relationVisitor->certificateNo,
            'applysex' => '',
            'applymobilePhone' => $relationVisitor->mobile,
            'applyAddress' => $relationVisitor->address,
            'certiStartDate' => $relationVisitor->certificateStartDate,
            'certiEndDate' => $relationVisitor->certificateEndDate,
            'applyProvinceCode' => $relationVisitor->provinceCode,
            'applyCityCode' => $relationVisitor->cityCode,
            'applyDistrictCode' => $relationVisitor->areaCode,
        ];

        if ($relationVisitor->typeCode == 'R1') {
            $certificateNo = $relationVisitor->certificateNo;
            $data['applybirthDate'] = date('Y-m-d', strtotime(substr($certificateNo, 6, 8)));
            $data['applysex'] = (substr($certificateNo,  -2, 1) % 2) ? '0' : '1';//0为男 1为女
        }
        if($relationVisitor->typeCode != 'R1'){
            $data['totalAssets'] = $this->visitor->getExtInfoVisitor()->argc['totalAssets'] ?? '';
            $data['operatingIncome'] = $this->visitor->getExtInfoVisitor()->argc['operatingIncome'] ?? '';
            $data['employeesNumber'] = $this->visitor->getExtInfoVisitor()->argc['employeesNumber'] ?? '';
            $data['mainBusiness'] = $this->visitor->getExtInfoVisitor()->argc['mainBusiness'] ?? '';
            $data['industryMainCategory'] = $this->getIndustryMainCategory($this->visitor->getExtInfoVisitor()->argc['industrySubCategory'] ?? '');
            $data['industrySubCategory'] = $this->visitor->getExtInfoVisitor()->argc['industrySubCategory'] ?? '';
        }

        return $data;
    }

    public function getIndustryMainCategory($type)
    {
        $typeCode = intval($type);
        if($typeCode <= 5){
            return 'A';
        }elseif($typeCode <= 11){
            return 'B';
        }elseif($typeCode <= 42){
            return 'C';
        }elseif($typeCode <= 46){
            return 'D';
        }elseif($typeCode <= 50){
            return 'E';
        }elseif($typeCode <= 52){
            return 'F';
        }elseif($typeCode <= 60){
            return 'G';
        }elseif($typeCode <= 62){
            return 'H';
        }elseif($typeCode <= 65){
            return 'I';
        }elseif($typeCode <= 69){
            return 'J';
        }elseif($typeCode <= 70){
            return 'K';
        }elseif($typeCode <= 72){
            return 'L';
        }elseif($typeCode <= 74){
            return 'M';
        }elseif($typeCode <= 78){
            return 'N';
        }elseif($typeCode <= 82){
            return 'O';
        }elseif($typeCode <= 83){
            return 'P';
        }elseif($typeCode <= 85){
            return 'Q';
        }elseif($typeCode <= 90){
            return 'R';
        }elseif($typeCode <= 96){
            return 'S';
        }elseif($typeCode <= 97){
            return 'T';
        }else{
            return '';
        }

    }

    /**
     * 被保人
     * @return array
     */
    private function getInsureData(): array
    {
        $relationVisitor = $this->visitor->getInsuredVisitor();
        $data = [
            'insuredType' => $relationVisitor->typeCode == 'R1' ? '1' : '2',
            'insuredbirthDate' => '',
            'insuredName' => $relationVisitor->name,
            'insuredidentifyType' => TAICEnums::$certificateTypeCode[$relationVisitor->certificateTypeCode] ?? '',
            'insuredidentifyNumber' => $relationVisitor->certificateNo,
            'insuredsex' => '',
            'insuredmobilePhone' => $relationVisitor->mobile,
            'insuredAddress' => $relationVisitor->address,
            'certiStartDate' => $relationVisitor->certificateStartDate,
            'certiEndDate' => $relationVisitor->certificateEndDate,
            'insuredProvinceCode' => $relationVisitor->provinceCode,
            'insuredCityCode' => $relationVisitor->cityCode,
            'insuredDistrictCode' => $relationVisitor->areaCode,
        ];

        if ($relationVisitor->typeCode == 'R1') {
            $certificateNo = $relationVisitor->certificateNo;
            $data['insuredbirthDate'] = date('Y-m-d', strtotime(substr($certificateNo, 6, 8)));
            $data['insuredsex'] = (substr($certificateNo,  -2, 1) % 2) ? '0' : '1';//0为男 1为女
        }
        if($relationVisitor->typeCode != 'R1'){
            $data['totalAssets'] = $this->visitor->getExtInfoVisitor()->argc['totalAssets'] ?? '';
            $data['operatingIncome'] = $this->visitor->getExtInfoVisitor()->argc['operatingIncome'] ?? '';
            $data['employeesNumber'] = $this->visitor->getExtInfoVisitor()->argc['employeesNumber'] ?? '';
            $data['mainBusiness'] = $this->visitor->getExtInfoVisitor()->argc['mainBusiness'] ?? '';
            $data['industryMainCategory'] = $this->getIndustryMainCategory($this->visitor->getExtInfoVisitor()->argc['industrySubCategory'] ?? '');
            $data['industrySubCategory'] = $this->visitor->getExtInfoVisitor()->argc['industrySubCategory'] ?? '';
        }
        return $data;
    }

    /**
     * 车主
     * @return array
     */
    private function getOwnerData(): array
    {
        return [
            'carOwnerIdentifyType' => TAICEnums::$certificateTypeCode[$this->visitor->getOwnerVisitor()->certificateTypeCode] ?? '',
            'carOwnerIdentifyNumber' => $this->visitor->getOwnerVisitor()->certificateNo
        ];
    }

    private function getDeliveryData():array
    {
        $relationVisitor = $this->visitor->getOwnerVisitor();
        return [
            'acceptName' => $relationVisitor->name,
            'acceptTelephone' => $relationVisitor->mobile,
            'acceptProvince' => $relationVisitor->provinceCode,
            'acceptCity' => $relationVisitor->cityCode,
            'acceptTown' => $relationVisitor->areaCode,
            'acceptAddress' => $relationVisitor->address,
            'licenceNo' => $this->visitor->getVehicleVisitor()->licenseNo,
            'deliveryType' => 'DT0001',
            'appointmentTime' => date('Y-m-d 15:00:00',strtotime("+1 day"))
        ];
    }

    private function nonCar($arr)
    {
        if($this->visitor->getNonCarUnderwritingVisitor()->getNonCarList()){
            /**@var NonCarProductVisitor $single*/
            $single = $this->visitor->getNonCarUnderwritingVisitor()->getNonCarList()[0] ?? [];
            //非车产品列表
            $arr['rcldReqDtoList'] = [
                [
                    'rcldUwCount' => $single->insuredNum,
                    'rcldPlan' => $single->productCode.'-'.$single->planCode,
                    'rcldProductName' => $single->argc['productName'] ?? '',
                    'rcldProductAmt' => round($single->premium * $single->insuredNum,2),
                ]
//                [
//                    'rcldUwCount' => '1',
//                    'rcldPlan' => '1101-FB000100',
//                    'rcldProductName' => '苏锦程客车500元（车+X）',
//                    'rcldProductAmt' => '500.0000',
//                    'combinPlanCode' => '0000-ZH000023',
//                ],
//                [
//                    'rcldUwCount' => '1',
//                    'rcldPlan' => '0303-FB000099',
//                    'rcldProductName' => '苏车+X家财',
//                    'rcldProductAmt' => '100.0000',
//                    'combinPlanCode' => '0000-ZH000023',
//                ],

            ];
            $arr['rcldInd'] = '1';
            $arr['outerSystem'] = 'W151YN';
        }
        return $arr;
    }
}
